package com.dykj.apps.web.sysmanager.dao;

import java.util.ArrayList;
import java.util.List;

import com.dykj.apps.web.sysmanager.dto.SysUserDTO;
import com.dykj.platform.framework.common.paging.AbstractPQDAO;
import com.dykj.platform.framework.common.paging.Hquery;
import com.dykj.platform.framework.common.paging.PagingHelper;
import com.dykj.platform.framework.common.paging.Page;
import com.dykj.platform.framework.exception.DatabaseException;

public class UserManagerDAOImpl extends AbstractPQDAO implements
		IUserManagerDAO {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public Hquery createHQ(Object objDTO,Page page)throws DatabaseException{
		SysUserDTO dto = (SysUserDTO)objDTO;
		String type = dto.getType();
		Hquery hquery = new Hquery();
		List paramlist = new ArrayList();
		StringBuffer strBufHql = new StringBuffer();
		strBufHql.append("from SysUser u where 1=1 ");
		//用户ID
		strBufHql.append(PagingHelper.createWherePara("and","u.userid","=",dto.getP_userid(),paramlist));
		//部门
		strBufHql.append(PagingHelper.createWherePara("and","u.dept","like",dto.getP_dept(),paramlist));
		//是否领导
		strBufHql.append(PagingHelper.createWherePara("and","u.isleader","=",dto.getP_isleader(),paramlist));
		hquery.setParalist(paramlist);
		//group
		if("0".equals(type)){
	  //strBufHql.append("and u.groupid in (select g.groupid from SysGroup g where g.glevel = '1') ");	1		
		strBufHql.append("and u.groupid in (select g.groupid from SysGroup g where g.parentid is null or g.parentid = '191') ");
		}else{
		strBufHql.append("and u.groupid in (select g.groupid from SysGroup g where g.parentid = '"+dto.getGroupid()+"')");
		}
		hquery.setQueryString(strBufHql.toString());
		if(page.getSort()!=null){
			hquery.setSortOrder(page.getSort().getSortOrder());
			hquery.setOrderby(page.getSort().getProperty());
		}
		return hquery;
	}
	/**
	 * 查询已经有分配过角色的用户
	 */
	public List queryHaveRoleUser(Long roleid,String groupid) throws DatabaseException {
		String hql = "from SysUser s where s.roleid="+roleid;
		if(null!=groupid){
			hql = hql+"and s.groupid in (select g.groupid from SysGroup g where g.parentid = '"+groupid+"' )";
		}else{
			hql = hql+"and s.groupid in (select g.groupid from SysGroup g where g.parentid is null or g.parentid = '191')";
		  //hql = hql+"and s.groupid in (select g.groupid from SysGroup g where g.glevel = '1')";	2
		}
		return this.query(hql);
	}
	/**
	 * 查询没有角色的用户
	 */
	public List queryNoRoleUser(String groupid) throws DatabaseException {
		String hql = "from SysUser s where (s.roleid = 0 or s.roleid is null)";
		if(null!=groupid){
			hql = hql+"and s.groupid in (select g.groupid from SysGroup g where g.parentid = '"+groupid+"' )";
		}else{
			hql = hql+"and s.groupid in (select g.groupid from SysGroup g where g.parentid is null or g.parentid = '191')";
		  //hql = hql+"and s.groupid in (select g.groupid from SysGroup g where g.glevel = '1')";	3
		}
		return this.query(hql);
	}
	/**
	 * 取消用户角色
	 */
	public int updateUserRole(Long roleid)throws DatabaseException {
		String hql = "update SysUser s set s.roleid = 0 where s.roleid="+roleid;
		return this.execDML(hql);
	}
	/**
	 * 查询有数据权限的用户
	 */
	public List queryHaveAuthUser(Long authid) throws DatabaseException {
		String hql = "from SysUser s where s.authid="+authid;
		return this.query(hql);
	}
	/**
	 * 查询没有数据权限的用户
	 */
	public List queryNoAuthUser() throws DatabaseException {
		String hql = "from SysUser s where s.authid is null";
		return this.query(hql);
	}
	/**
	 * 新增权限时，查询待选user
	 */
	public List queryNoAuthUser(String type, String groupid) throws DatabaseException {
		//Hquery hquery = new Hquery();		
		StringBuffer sb = new StringBuffer();
		sb.append(" from SysUser sysuser where (sysuser.authid = '0' or sysuser.authid is null ) ");
		 if(type.equals("0")){
		sb.append(" and sysuser.groupid in(select sysgroup.groupid from SysGroup sysgroup where sysgroup.glevel ='1')");
		 }else{
			 sb.append(" and sysuser.groupid in( select sysgroup.groupid from SysGroup sysgroup where sysgroup.parentid= '")
				.append(groupid).append("' ) ");
		 }
		 String hql = sb.toString();
		 return this.query(hql);
	}
	/**
	 * 修改权限的时候，查询已选user
	 */
	public List queryUserByAuth(String authid, String type, String groupid) throws DatabaseException {
		StringBuffer sb = new StringBuffer();
		sb.append("from SysUser s where s.authid = ");
		sb.append("'"+Long.parseLong(authid)+"'");
		if("0".equals(type)){//超管
			sb.append("and s.groupid in (select sysgroup.groupid from SysGroup sysgroup where sysgroup.glevel='1')");
		}else{ //二管
		sb.append(" and s.groupid in ( select sysgroup.groupid from SysGroup sysgroup where sysgroup.parentid = '")
		.append(groupid).append("' )");
		 }
		String hql = sb.toString();
		return this.query(hql);
	}	
}
